package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.mapper.mwsreport.IMwsInventoryAgeMapper;
import com.yunchang.springboot.models.mwsreportbean.MwsInventoryAge;
import com.yunchang.springboot.models.mwsreportbean.MwsInventoryAgeDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:52
 * @description:
 */
@Component
public class InventoryAgeDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    @Autowired
    IMwsInventoryAgeMapper mwsInventoryAgeMapper;
    /**
     * 库龄   将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        int snapshotDateIndex = Integer.parseInt(titleMap.get("snapshot-date").toString());
        //获取数据库里面最新的日期
        String latestDay = mwsInventoryAgeMapper.getLatestDay(shopBrandName, area);
        if (!StringUtil.isBlank(latestDay)) {
            List<String> firstList = lists.get(1);
            //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
            if (DateUtil.compateTime(latestDay, firstList.get(snapshotDateIndex)) >= 0) {
                return null;
            }
        }
        int skuIndex = Integer.parseInt(titleMap.get("sku").toString());
        int fnskuIndex = Integer.parseInt(titleMap.get("fnsku").toString());
        int asinIndex = Integer.parseInt(titleMap.get("asin").toString());
        int productNameIndex = Integer.parseInt(titleMap.get("product-name").toString());
        int conditionIndex = Integer.parseInt(titleMap.get("condition").toString());
        int avaliableQuantitySellableIndex = Integer.parseInt(titleMap.get("avaliable-quantity(sellable)").toString());
        int qtyWithRemovalsInProgressIndex = Integer.parseInt(titleMap.get("qty-with-removals-in-progress").toString());
        int invAge0To90DaysIndex = Integer.parseInt(titleMap.get("inv-age-0-to-90-days").toString());
        int invAge91To180DaysIndex = Integer.parseInt(titleMap.get("inv-age-91-to-180-days").toString());
        int invAge181To270DaysIndex = Integer.parseInt(titleMap.get("inv-age-181-to-270-days").toString());
        int invAge271To365DaysIndex = Integer.parseInt(titleMap.get("inv-age-271-to-365-days").toString());
        int invAge365PlusDaysIndex = Integer.parseInt(titleMap.get("inv-age-365-plus-days").toString());
        int currencyIndex = Integer.parseInt(titleMap.get("currency").toString());
        int qtyToBeChargedLtsf6MoIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("qty-to-be-charged-ltsf-6-mo")) {
            qtyToBeChargedLtsf6MoIndex = Integer.parseInt(titleMap.get("qty-to-be-charged-ltsf-6-mo").toString());
        }
        int projectedLtsf6MoIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("projected-ltsf-6-mo")) {
            projectedLtsf6MoIndex = Integer.parseInt(titleMap.get("projected-ltsf-6-mo").toString());
        }
        int qtyToBeChargedLtsf12MoIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("qty-to-be-charged-ltsf-12-mo")) {
            qtyToBeChargedLtsf12MoIndex = Integer.parseInt(titleMap.get("qty-to-be-charged-ltsf-12-mo").toString());
        }
        int projectedLtsf12MoIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("projected-ltsf-12-mo")) {
            projectedLtsf12MoIndex = Integer.parseInt(titleMap.get("projected-ltsf-12-mo").toString());
        }
        int unitsShippedLast7DaysIndex = Integer.parseInt(titleMap.get("units-shipped-last-7-days").toString());
        int unitsShippedLast30DaysIndex = Integer.parseInt(titleMap.get("units-shipped-last-30-days").toString());
        int unitsShippedLast60DaysIndex = Integer.parseInt(titleMap.get("units-shipped-last-60-days").toString());
        int unitsShippedLast90DaysIndex = Integer.parseInt(titleMap.get("units-shipped-last-90-days").toString());
        int alertIndex = Integer.parseInt(titleMap.get("alert").toString());
        int yourPriceIndex = Integer.parseInt(titleMap.get("your-price").toString());
        int salesPriceIndex = Integer.parseInt(titleMap.get("sales_price").toString());
        int lowestPriceNewIndex = Integer.parseInt(titleMap.get("lowest_price_new").toString());
        int lowestPriceUsedIndex = Integer.parseInt(titleMap.get("lowest_price_used").toString());
        int recommendedActionIndex = Integer.parseInt(titleMap.get("Recommended action").toString());
        int healthyInventoryLevelIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("Healthy Inventory Level")) {
            healthyInventoryLevelIndex = Integer.parseInt(String.valueOf(titleMap.get("Healthy Inventory Level")));
        }
        int recommendedSalesPriceIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("Recommended sales price")) {
            recommendedSalesPriceIndex = Integer.parseInt(String.valueOf(titleMap.get("Recommended sales price")));
        }
        int recommendedSaleDurationIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("Recommended sale duration (days)")) {
            recommendedSaleDurationIndex = Integer.parseInt(String.valueOf(titleMap.get("Recommended sale duration (days)")));
        }
        int recommendedRemovalQuantityIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("Recommended Removal Quantity")) {
            recommendedRemovalQuantityIndex = Integer.parseInt(String.valueOf(titleMap.get("Recommended Removal Quantity")));
        }
        int estimatedCoseSavingsOfRemovalIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("Estimated cost savings of removal")) {
            estimatedCoseSavingsOfRemovalIndex = Integer.parseInt(String.valueOf(titleMap.get("Estimated cost savings of removal")));
        }
        int sellThroughIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("sell-through")) {
            sellThroughIndex = Integer.parseInt(String.valueOf(titleMap.get("sell-through")));
        }
        int cubicFeetIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("cubic-feet")) {
            cubicFeetIndex = Integer.parseInt(String.valueOf(titleMap.get("cubic-feet")));
        }
        int storageTypeIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("storage-type")) {
            storageTypeIndex = Integer.parseInt(String.valueOf(titleMap.get("storage-type")));
        }
        List<Object> list = new ArrayList<>(lists.size());
        for (int i = 1; i < lists.size(); i++) {
            MwsInventoryAgeDO inventoryAge = new MwsInventoryAgeDO();
            List<String> stringList = lists.get(i);
            String currencySign = stringList.get(currencyIndex);
            for (int index = 0; index < stringList.size(); index++) {
                if (stringList.get(index).contains(currencySign)) {
                    if (stringList.get(index).equals(currencySign)) {
                        continue;
                    }
                    stringList.set(index, stringList.get(index).substring(currencySign.length() + 1));
                }
            }
            inventoryAge.setShop(shopBrandName);
            inventoryAge.setSellerId(sellerId);
            inventoryAge.setArea(area);
            inventoryAge.setSnapshotDate(stringList.get(snapshotDateIndex));
            inventoryAge.setSku(stringList.get(skuIndex));
            inventoryAge.setFnsku(stringList.get(fnskuIndex));
            inventoryAge.setAsin(stringList.get(asinIndex));
            inventoryAge.setProductName(stringList.get(productNameIndex));
            inventoryAge.setGoodsCondition(stringList.get(conditionIndex));
            inventoryAge.setAvaliableQuantitySellable(String.valueOf(stringList.get(avaliableQuantitySellableIndex)));
            inventoryAge.setQtyWithRemovalsInProgress(String.valueOf(stringList.get(qtyWithRemovalsInProgressIndex)));
            inventoryAge.setInvAge0To90Days(String.valueOf(stringList.get(invAge0To90DaysIndex)));
            inventoryAge.setInvAge91To180Days(String.valueOf(stringList.get(invAge91To180DaysIndex)));
            inventoryAge.setInvAge181To270Days(String.valueOf(stringList.get(invAge181To270DaysIndex)));
            inventoryAge.setInvAge271To365Days(String.valueOf(stringList.get(invAge271To365DaysIndex)));
            inventoryAge.setInvAge365PlusDays(String.valueOf(stringList.get(invAge365PlusDaysIndex)));
            inventoryAge.setCurrency(stringList.get(currencyIndex));
            if (qtyToBeChargedLtsf6MoIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setQtyToBeChargedLtsf6Mo(String.valueOf(stringList.get(qtyToBeChargedLtsf6MoIndex)));
            }
            if (projectedLtsf6MoIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setProjectedLtsf6Mo(String.valueOf(stringList.get(projectedLtsf6MoIndex)));
            }
            if (qtyToBeChargedLtsf12MoIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setQtyToBeChargedLtsf12Mo(String.valueOf(stringList.get(qtyToBeChargedLtsf12MoIndex)));
            }
            if (projectedLtsf12MoIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setProjectedLtsf12Mo(String.valueOf(stringList.get(projectedLtsf12MoIndex)));
            }
            inventoryAge.setUnitsShippedLast7Days(String.valueOf(stringList.get(unitsShippedLast7DaysIndex)));
            inventoryAge.setUnitsShippedLast30Days(String.valueOf(stringList.get(unitsShippedLast30DaysIndex)));
            inventoryAge.setUnitsShippedLast60Days(String.valueOf(stringList.get(unitsShippedLast60DaysIndex)));
            inventoryAge.setUnitsShippedLast90Days(String.valueOf(stringList.get(unitsShippedLast90DaysIndex)));
            inventoryAge.setAlert(stringList.get(alertIndex));
            inventoryAge.setYourPrice(stringList.get(yourPriceIndex));
            inventoryAge.setSalesPrice(stringList.get(salesPriceIndex));
            inventoryAge.setLowestPriceNew(String.valueOf(stringList.get(lowestPriceNewIndex)));
            inventoryAge.setLowestPriceUsed(String.valueOf(stringList.get(lowestPriceUsedIndex)));
            inventoryAge.setRecommendedAction(stringList.get(recommendedActionIndex));
            inventoryAge.setCreatTime(DateUtil.getDate());
            inventoryAge.setUpdateTime(DateUtil.getDate());
            if (healthyInventoryLevelIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setHealthyInventoryLevel(String.valueOf(stringList.get(healthyInventoryLevelIndex)));
            }
            if (recommendedSalesPriceIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setRecommendedSalesPrice(String.valueOf(stringList.get(recommendedSalesPriceIndex)));
            }
            if (recommendedSaleDurationIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setRecommendedSaleDurationDays(String.valueOf(stringList.get(recommendedSaleDurationIndex)));
            }
            if (recommendedRemovalQuantityIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setRecommendedRemovalQuantity(String.valueOf(stringList.get(recommendedRemovalQuantityIndex)));
            }
            if (estimatedCoseSavingsOfRemovalIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setEstimatedCostSavingsOfRemoval(stringList.get(estimatedCoseSavingsOfRemovalIndex));
            }
            if (sellThroughIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setSellThrough(stringList.get(sellThroughIndex));
            }
            if (cubicFeetIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setCubicFeet(stringList.get(cubicFeetIndex));
            }
            if (storageTypeIndex != WRONG_NUMBER_SIGN) {
                inventoryAge.setStorageType(stringList.get(storageTypeIndex));
            }
            inventoryAge.setFinanceStatus(1);
            list.add(inventoryAge);
        }
        return list;
    }
}
